package org.yelongframework.spring.web.multipart;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.yelongframework.util.Assert;

/**
 * 默认实现
 * 
 * @since 1.0.0
 */
public class DefaultMultipartRequestSupport implements MultipartRequestSupport {

	private final MultipartResolver multipartResolver;

	public DefaultMultipartRequestSupport(MultipartResolver multipartResolver) {
		Assert.notNull(multipartResolver, "multipartResolver cannot be null");
		this.multipartResolver = multipartResolver;
	}

	@Override
	public MultipartRequest getMultipartRequest(HttpServletRequest request) {
		if (!multipartResolver.isMultipart(request)) {
			return null;
		}
		return multipartResolver.resolveMultipart(request);
	}

	@Override
	public MultipartFile getMultipartFile(HttpServletRequest request, String name) {
		MultipartRequest multipartRequest = getMultipartRequest(request);
		return null == multipartRequest ? null : multipartRequest.getFile(name);
	}

	@Override
	public List<MultipartFile> getMultipartFiles(HttpServletRequest request, String name) {
		MultipartRequest multipartRequest = getMultipartRequest(request);
		return null == multipartRequest ? null : multipartRequest.getFiles(name);
	}

	@Override
	public Map<String, List<MultipartFile>> getMultipartFilesMap(HttpServletRequest request, String name) {
		MultipartRequest multipartRequest = getMultipartRequest(request);
		return null == multipartRequest ? null : multipartRequest.getMultiFileMap();
	}

}
